﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace SpeechSynthesizer
{
    /// <summary>
    /// http传输数据方法类
    /// 20190422 zfx
    /// </summary>
    public static class HttpTransmission
    {

        /// <summary>
        /// 获取图片流数据及处理
        /// </summary>
        /// <returns>返回处理后的图片地址/image/</returns>
        public static string GetImage(string dataParam, string url)
        {
            #region
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            // Encode the data  
            byte[] encodedBytes = Encoding.UTF8.GetBytes(dataParam);
            myHttpWebRequest.ContentLength = encodedBytes.Length;

            // Write encoded data into request stream  
            Stream requestStream = myHttpWebRequest.GetRequestStream();
            requestStream.Write(encodedBytes, 0, encodedBytes.Length);
            requestStream.Close();

            HttpWebResponse result;

            try
            {
                result = (HttpWebResponse)myHttpWebRequest.GetResponse();
            }
            catch
            {
                return string.Empty;
            }

            if (result.StatusCode == HttpStatusCode.OK)
            {


                //Stream mystream = result.GetResponseStream();
                //byte[] tt = StreamToBytes(mystream);
                //string imgName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
                ////文件存储相对于当前应用目录的虚拟目录
                //string path = "/image/";
                ////获取相对于应用的基目录,创建目录
                //string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path;     //通过此对象获取文件名
                //System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(path + imgName), tt);//讲byte[]存储为图片
                //return "/image/" + imgName;
            }
            return null;
            #endregion
        }

        /// <summary>
        /// 将流文件转为字节组
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            #region
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            return bytes.ToArray();
            #endregion
        }

        /// <summary>
        /// http文本获取get方法
        /// </summary>
        /// <param name="xmlParam">发送数据</param>
        /// <param name="url">发送地址</param>
        /// <returns>接收数据字符串</returns>
        public static Stream GetHttpResponse(string xmlParam, string url)
        {
            #region 文本获取get方法
            //Encoding myEncoding = Encoding.GetEncoding("utf-8");
            string dataUrl = "";
            //if (xmlParam != null & xmlParam != "")
            //{
            //    dataUrl = url + "?" + HttpUtility.UrlEncode(xmlParam);
            //}
            //else
            //{
                dataUrl = url;
            //}
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(dataUrl);
            myHttpWebRequest.Method = "GET";
            HttpWebResponse result;
            try
            {
                result = (HttpWebResponse)myHttpWebRequest.GetResponse();

            }
            catch(Exception ex)
            {
                return null;
            }
            if (result.StatusCode == HttpStatusCode.OK)
            {
                return result.GetResponseStream();
                
            }
            return null;
            #endregion
        }

        /// <summary>  
        /// 模拟POST提交  
        /// </summary>  
        /// <param name="url">请求地址</param>  
        /// <param name="xmlParam">xml参数</param>  
        /// <returns>返回结果</returns>  
        public static string PostHttpResponse(string xmlParam, string url)
        {
            #region 文本获取post方法
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            // Encode the data  
            byte[] encodedBytes = Encoding.UTF8.GetBytes(xmlParam);
            myHttpWebRequest.ContentLength = encodedBytes.Length;

            // Write encoded data into request stream  
            Stream requestStream = myHttpWebRequest.GetRequestStream();
            requestStream.Write(encodedBytes, 0, encodedBytes.Length);
            requestStream.Close();

            HttpWebResponse result;

            try
            {
                result = (HttpWebResponse)myHttpWebRequest.GetResponse();
            }
            catch
            {
                return string.Empty;
            }

            if (result.StatusCode == HttpStatusCode.OK)
            {

                Stream mystream = result.GetResponseStream();
                using (StreamReader reader = new StreamReader(mystream))
                {
                    return reader.ReadToEnd();
                }


            }
            return null;
            #endregion
        }
    }
}
